package com.hllxd.hism.star.config.mvc;

import com.bj58.a.filters.*;
import com.bj58.sso.session.servlet.listener.cache.CacheEngineLoadListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xiyong.lxy
 * @date 2022.04.14
 * SSO注解方式配置文件
 * passport.sso -enable表示自动开启是sso用户识别, 会自动加载
 * 针对所有WEB的接口进行拦截
 * 需要将config_sandbox中的配置进行copy安装，参照bin中cloud_start.sh
 */

@Configuration
@ConditionalOnProperty(name = "passport.sso", havingValue = "enable")
public class SsoInterceptor {
    @Bean
    public ServletListenerRegistrationBean<CacheEngineLoadListener> cacheEngineLoadListener(){
        ServletListenerRegistrationBean<CacheEngineLoadListener> cacheEngineLoadListener = new ServletListenerRegistrationBean<CacheEngineLoadListener>();
        cacheEngineLoadListener.setListener(new CacheEngineLoadListener());
        cacheEngineLoadListener.setOrder(1);
        return cacheEngineLoadListener;
    }

    @Bean
    public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> singleSignOutHttpSessionListener(){
        ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> cacheEngineLoadListener = new ServletListenerRegistrationBean<SingleSignOutHttpSessionListener>();
        cacheEngineLoadListener.setListener(new SingleSignOutHttpSessionListener());
        cacheEngineLoadListener.setOrder(2);
        return cacheEngineLoadListener;
    }

    @Bean
    public FilterRegistrationBean cacheSessionFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new A1_CacheSessionFilter());
        registration.addUrlPatterns("/*");
        registration.setName("CacheSessionFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean characterEncodingFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new A2_CharacterEncodingFilter());
        registration.addUrlPatterns("/*");
        registration.setName("CharacterEncodingFilter");
        registration.setOrder(2);
        return registration;
    }

    @Bean
    public FilterRegistrationBean singleSignOutFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new A3_SingleSignOutFilter());
        registration.addUrlPatterns("/*");
        registration.setName("SingleSignOutFilter");
        registration.setOrder(3);
        return registration;
    }

    @Bean
    public FilterRegistrationBean authenticationFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new A4_AuthenticationFilter());
        registration.addUrlPatterns("/*");
        registration.setName("AuthenticationFilter");
        registration.setOrder(4);
        return registration;
    }

    @Bean
    public FilterRegistrationBean proxyReceivingTicketValidationFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new A5_ProxyReceivingTicketValidationFilter());
        registration.addUrlPatterns("/*");
        registration.setName("ProxyReceivingTicketValidationFilter");
        registration.setOrder(5);
        return registration;
    }

    @Bean
    public FilterRegistrationBean ssoHttpServletRequestWrapperFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new A6_SSOHttpServletRequestWrapperFilter());
        registration.addUrlPatterns("/*");
        registration.setName("SSOHttpServletRequestWrapperFilter");
        registration.setOrder(6);
        return registration;
    }

    @Bean
    public FilterRegistrationBean assertionThreadLocalFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new A7_AssertionThreadLocalFilter());
        registration.addUrlPatterns("/*");
        registration.setName("AssertionThreadLocalFilter");
        registration.setOrder(7);
        return registration;
    }
}
